package com.caowj.composedemo.widget


import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import coil.annotation.ExperimentalCoilApi
import com.google.accompanist.pager.ExperimentalPagerApi
import com.caowj.composedemo.composable.*
import com.caowj.composedemo.util.StatsBarUtil

/**
 * 内容 导航
 * 参考：https://github.com/linxiangcheer/PlayAndroid
 */
@OptIn(ExperimentalMaterialApi::class)
@ExperimentalCoilApi
@ExperimentalPagerApi
@Composable
fun NavigationHost(
    navHostController: NavHostController,
    paddingValues: PaddingValues = PaddingValues(0.dp),
    onFinish: () -> Unit
) {

    val context = LocalContext.current

    NavHost(
        navHostController,
        startDestination = KeyNavigationRoute.MAIN.routeStr,//指向主页面
        //todo 只有主界面需要这个padding
        modifier = Modifier.padding(paddingValues),
        builder = {
            //主页面（底部菜单对应的内容）
            navigation(
                route = KeyNavigationRoute.MAIN.routeStr,//自定义主页面
                startDestination = Nav.BottomNavScreen.LearnScreen.route//导航开始页面的内容（和Nav.getBottomNavRoute保持一致）
            ) {
                //1、首页
                composable(Nav.BottomNavScreen.HomeScreen.route) {
                    //系统颜色的状态栏
                    StatsBarUtil().StatsBarColor(false)

                    HomeCompose(navHostController)

                    //点击两次返回才关闭app
                    BackHandler {
                        TwoBackFinish().execute(context, onFinish)
                    }
                }

                //2、学习页面
                composable(Nav.BottomNavScreen.LearnScreen.route) {
                    //系统颜色的状态栏
                    StatsBarUtil().StatsBarColor(false)

                    LearnCompose(navHostController)

                    //点击两次返回才关闭app
                    BackHandler {
                        TwoBackFinish().execute(context, onFinish)
                    }
                }
            }

// ------------> Content中对应的内容 <---------------

            //登录页面
            composable(route = KeyNavigationRoute.LOGIN.routeStr) {
                //透明/沉浸式状态栏
                StatsBarUtil().StatsBarColor(true)

                LoginCompose(navHostController)

                BackHandler { navHostController.navigateUp() }
            }

//            //H5页面
//            composable(
//                route = "${KeyNavigationRoute.WEBVIEW.route}?url={url}", arguments = listOf(
//                    navArgument("url") { defaultValue = "https://www.wanandroid.com/" })
//            ) { backStackEntry ->
//
//                //系统颜色的状态栏
//                StatsBarUtil().StatsBarColor(false)
//
//                WebViewCompose(
//                    navHostController,
//                    backStackEntry.arguments?.getString("url") ?: "https://www.wanandroid.com"
//                )
//
//                BackHandler { navHostController.navigateUp() }
//            }

            //手势页面
            composable(route = KeyNavigationRoute.GESTURES.routeStr) {
                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

//                TransformableSample(navHostController)
                swipeableSample2()

                BackHandler { navHostController.navigateUp() }
            }
            //状态页面
            composable(route = KeyNavigationRoute.STATE.routeStr) {
                //系统颜色的状态栏
                StatsBarUtil().StatsBarColor(false)

//                MyScreenContent()
                TestState()

                BackHandler { navHostController.navigateUp() }
            }
        }
    )
}

/**
 * 页面跳转关键类
 */
enum class KeyNavigationRoute(
    val routeStr: String
) {
    //主页面
    MAIN("main"),

    //登录页面
    LOGIN("login"),

    //注册页面
    REGISTER("register"),

    //手势
    GESTURES("learn_gestures"),

    //状态管理
    STATE("learn_state")
}










